<template>
  <div class="container">
    <el-form
      :model="addForm"
      :rules="addFormRules"
      ref="addFormRef"
      label-width="100px"
    >
      <el-form-item label="所属学科" prop="subjectID" v-if="!liststate">
        <el-select
          v-model="addForm.subjectID"
          placeholder="请选择"
          style="width:100%"
        >
          <el-option
            v-for="item in options1"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- 目录名称 -->
      <el-form-item label="目录名称" prop="directoryName">
        <el-input
          v-model="addForm.directoryName"
          placeholder="请输入目录名称"
        ></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects'
import { detail, add, update } from '@/api/hmmm/directorys'
export default {
  props: {
    id: {
      type: [Number, String],
      required: true
    },
    liststate: {
      type: Boolean,
      required: true
    },
    subjectID: {
      type: [Number, String],
      required: true
    }
  },
  data() {
    return {
      addForm: {
        subjectID: '',
        directoryName: ''
      },
      addFormRules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      },
      options1: []
    }
  },
  created() {
    this.getsubjectslist()
    if (this.id) {
      this.getsubjects()
    }
  },
  mounted() {},
  methods: {
    async getsubjectslist() {
      try {
        const { data } = await simple()
        this.options1 = data
      } catch (err) {
        this.$message.error('获取失败')
      }
    },
    async getsubjects() {
      try {
        const { data } = await detail({ id: this.id })
        console.log(data)
        this.addForm = data
      } catch (err) {
        this.$message.error('获取失败')
      }
    },
    btnclick() {
      if (this.liststate) {
        this.addForm.subjectID = parseInt(this.subjectID)
      }
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        if (!this.id) {
          try {
            await add(this.addForm)
            this.$message.success('添加成功')
            this.$emit('close')
          } catch (err) {
            this.$message.error('添加失败')
          }
        } else {
          try {
            await update({
              id: this.id,
              subjectID: this.addForm.subjectID,
              directoryName: this.addForm.directoryName
            })
            this.$message.success('修改成功')
            this.$emit('close')
          } catch (err) {
            this.$message.error('修改失败')
          }
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped></style>
